package com.example.mywechat;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    //生产4个Fragment控制器
    private Fragment fragmentWeiXin = new WeiXinFragment();
    private Fragment fragmentFriend = new FriendFragment();
    private Fragment fragmentAddress = new AddressFragment();
    private Fragment fragmentSetting = new SettingFragment();

    //四个按钮
    private LinearLayout buttonWeiXin;
    private LinearLayout buttonFriend;
    private LinearLayout buttonAddress;
    private LinearLayout buttonSetting;

    private ImageView imageButtonWeiXin;
    private ImageView ImageButtonFriend;
    private ImageView ImageButtonAddress;
    private ImageView ImageButtonSetting;

    //表示Fragment进行通讯的控制器
    private FragmentManager fm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragment();
        initEvent();
        setFragment(0);

//        final Intent intent=new Intent(this,PyqActivity.class);
//        Button button_pyq=getSupportFragmentManager().findFragmentById(R.id.id_fragment_setting).getView().findViewById(R.id.pyqBtn);
//        System.out.println("创建了"+button_pyq);
//        button_pyq.setOnClickListener(new View.OnClickListener(){
//            @Override
//            public void onClick(View view){
//                startActivity(intent);
//            }
//        });
    }



    private void initView(){
        buttonWeiXin = findViewById(R.id.id_button_weixin);
        buttonFriend = findViewById(R.id.id_button_friend);
        buttonAddress =  findViewById(R.id.id_button_address);
        buttonSetting =  findViewById(R.id.id_button_setting);

        imageButtonWeiXin = findViewById(R.id.id_button_weixin_img);
        ImageButtonFriend = findViewById(R.id.id_button_friend_img);
        ImageButtonAddress = findViewById(R.id.id_button_address_img);
        ImageButtonSetting = findViewById(R.id.id_button_setting_img);
    }

    private void initFragment(){
        // getFragmentManager() 版本问题
        fm = getSupportFragmentManager();
        // 控制器实例
        FragmentTransaction transaction = fm.beginTransaction();
        // R.id.id_content -> 选定主布局中的FrameLayout
        transaction.add(R.id.main_content, fragmentWeiXin);
        transaction.add(R.id.main_content, fragmentFriend);
        transaction.add(R.id.main_content, fragmentAddress);
        transaction.add(R.id.main_content, fragmentSetting);
        transaction.commit();
    }

    private void initEvent(){
        buttonWeiXin.setOnClickListener(this);
        buttonFriend.setOnClickListener(this);
        buttonAddress.setOnClickListener(this);
        buttonSetting.setOnClickListener(this);
    }

    private void setFragment(int i){
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        // 把图片设置为亮
        // 设置内容区域
        switch (i) {
            case 0:
                Log.d("setSelect","1");
                transaction.show(fragmentWeiXin);
                imageButtonWeiXin.setImageResource(R.drawable.tab_weixin_pressed);
                break;
            case 1:
                transaction.show(fragmentFriend);
                ImageButtonFriend.setImageResource(R.drawable.tab_find_frd_pressed);
                break;
            case 2:
                transaction.show(fragmentAddress);
                ImageButtonAddress.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                transaction.show(fragmentSetting);
                ImageButtonSetting.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(fragmentWeiXin);
        transaction.hide(fragmentFriend);
        transaction.hide(fragmentAddress);
        transaction.hide(fragmentSetting);
    }

    private void resetImg(){
        imageButtonWeiXin.setImageResource(R.drawable.tab_weixin_normal);
        ImageButtonFriend.setImageResource(R.drawable.tab_find_frd_normal);
        ImageButtonAddress.setImageResource(R.drawable.tab_address_normal);
        ImageButtonSetting.setImageResource(R.drawable.tab_settings_normal);
    }

    @Override
    public void onClick(View v) {
        Log.d("onClick","1");
        resetImg();
        switch (v.getId()){
            case R.id.id_button_weixin:
                Log.d("onClick_WeiXin","0");
                setFragment(0);
                break;
            case R.id.id_button_friend:
                Log.d("onClick_Frd","1");
                setFragment(1);
                break;
            case R.id.id_button_address:
                Log.d("onClick_contact","2");
                setFragment(2);
                break;
            case R.id.id_button_setting:
                Log.d("onClick_settings","3");
                setFragment(3);
                break;
            default:
                break;
        }
    }
}
